package com.hmy.finance.manager.workflow;


import com.alibaba.fastjson.JSON;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.workflow.api.bo.request.process.ProcessAddVariablesRequest;
import com.hmy.workflow.api.bo.request.process.RevokeProcessRequest;
import com.hmy.workflow.api.bo.request.process.StartProcessInstanceRequest;
import com.hmy.workflow.api.bo.response.process.StartProcessInstanceResponse;
import com.hmy.workflow.api.client.process.FlowProcessClient;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class FlowProcessClientManager {
    @Resource
    private FlowProcessClient flowProcessClient;

    /**
     * 创建流程
     *
     * @param startProcessInstanceRequest startProcessInstanceRequest
     */
    public StartProcessInstanceResponse start(StartProcessInstanceRequest startProcessInstanceRequest) {
        log.info("flowProcessClient#start param is:{}", JSON.toJSONString(startProcessInstanceRequest));
        FeignResult<StartProcessInstanceResponse> result;
        try{
            result = flowProcessClient.start(startProcessInstanceRequest);
        }catch(Exception e){
            log.error("flowProcessClient#start is error", e);
            throw new HMYRuntimeException(506900002);
        }
        if (Objects.isNull(result)) {
            log.error("flowProcessClient#startresult is null");
            throw new HMYRuntimeException("flowProcessClient#start result is null");
        }
        log.info("flowProcessClient#start result is:{}", JSON.toJSONString(result));
        if (result.isFailed()) {
            throw new HMYRuntimeException(result.getMsgCode(), result.getErrorText());
        }
        if (Objects.isNull(result.getData().getProcessInstanceId())) {
            throw new HMYRuntimeException(506900002);
        }
        return result.getData();
    }

    /**
     * 撤回流程审批申请
     *
     * @param revokeProcessRequest revokeProcessRequest
     */
    public Boolean revoke(RevokeProcessRequest revokeProcessRequest) {
        log.info("flowProcessClient#revoke param is:{}", JSON.toJSONString(revokeProcessRequest));
        FeignResult<Boolean> result;
        try{
            result = flowProcessClient.revoke(revokeProcessRequest);
        }catch(Exception e){
            log.error("flowProcessClient#revoke is error", e);
            throw new HMYRuntimeException(506900001);
        }
        if (Objects.isNull(result)) {
            log.error("flowProcessClient#revoke result is null");
            throw new HMYRuntimeException("flowProcessClient#revoke result is null");
        }
        log.info("flowProcessClient#revoke result is:{}", JSON.toJSONString(result));
        if (result.isFailed()) {
            throw new HMYRuntimeException(result.getMsgCode(), result.getErrorText());
        }
        return result.getData();
    }


    /**
     * 删除流程审批实例
     *
     * @param processInstanceId processInstanceId
     */
    public Boolean removeProcessInstance(String processInstanceId, Map<String, Object> variables) {
        log.info("flowProcessClient#removeV2 processInstanceId :{}, variables:{} ",processInstanceId, JSON.toJSONString(variables));
        FeignResult<Boolean> result;
        try{
            result = flowProcessClient.removeV2(processInstanceId, variables);
        }catch(Exception e){
            log.error("flowProcessClient#remove is error", e);
            throw new HMYRuntimeException(506900001);
        }
        if (Objects.isNull(result)) {
            log.error("flowProcessClient#remove result is null");
            throw new HMYRuntimeException("flowProcessClient#remove result is null");
        }
        log.info("flowProcessClient#remove result is:{}", JSON.toJSONString(result));
        if (result.isFailed()) {
            throw new HMYRuntimeException(result.getMsgCode(), result.getErrorText());
        }
        return result.getData();
    }

    /**
     * 修改流程变量
     *
     * @param processInstanceId 流程实例ID
     * @param variables         流程变量
     * @return 成功与否
     * @author songkf
     */
    public Boolean addVariables(String processInstanceId, Map<String, Object> variables) {
        ProcessAddVariablesRequest request = new ProcessAddVariablesRequest(processInstanceId, variables);
        log.info("flowProcessClient#addVariables param is:{}", JSON.toJSONString(request));
        FeignResult<Boolean> result;
        try{
            result = flowProcessClient.addVariables(request);
        }catch(Exception e){
            log.error("flowProcessClient#addVariables is error", e);
            throw new HMYRuntimeException(506900001);
        }
        if (Objects.isNull(result)) {
            log.error("flowProcessClient#addVariables result is null");
            throw new HMYRuntimeException("flowProcessClient#addVariables result is null");
        }
        log.info("flowProcessClient#addVariables result is:{}", JSON.toJSONString(result));
        if (result.isFailed()) {
            throw new HMYRuntimeException(result.getMsgCode(), result.getErrorText());
        }
        return result.getData();
    }

}
